import {
  selectUserPhone
} from "@/api/realAssets/icsharedstation/home";
import { getPostSiteList } from "@/api/realAssets/icsharedstation/getParent";
import { setpersonalInformation } from "./RYXX";
import { setandroidCode } from "./auth";
import { Dialog } from "vant";
import router from '@/router'

export function testUserMessage(siteName) {
  return new Promise((resolve, reject) => {
    const userInfo = JSON.parse(sessionStorage.getItem("userInfo"))
    setandroidCode(userInfo.userId);
    const routeName = router.currentRoute.name || ""
    localStorage.setItem("siteName", siteName)
    localStorage.setItem("routeName", routeName)
    selectUserPhone({
      ishid: userInfo.userId,
    }).then((res) => {
      if (res.data == "用户id错误") {
        Dialog.confirm({
          title: "当前用户未注册",
          message: "是否立即注册",
        })
          .then(() => {
            router.replace({ name: "i_life_userLogin" });
            reject(new Error("用户未注册"));
          })
          .catch(() => {
            router.replace({
              path: "/mainFrame/home"
            })
            reject(new Error("用户取消注册"));
            router.back();
          });
        setpersonalInformation("");
      } else {
        if (res.status == 500) {
          setpersonalInformation("");
          reject(new Error("服务器错误"));
          return;
        }
        setpersonalInformation(res.data);
        getPostSiteList({
          postId: res.data.postId,
          siteName: siteName,
          pageCurrent: 1,
          pageSize: 1
        }).then((res) => {
          if (res.resultMessage == "SUCCESS" && res.resultCode == 1) {
            localStorage.setItem("siteData", JSON.stringify(res.data[0]));
            resolve(res.data); // 返回 siteData
          } else {
            reject(new Error("获取站点数据失败"));
          }
        }).catch(reject)
      }
    }).catch(reject)
  })
}
